package com.auerdi.ui.detail.cutprice.detail.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.auerdi.R;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.BargainDatailResp;
import com.example.egghttp.retrofit.beans.banner.BargainRankListRES;
import com.example.egghttp.retrofit.beans.goods.CutProduct;
import com.example.egghttp.retrofit.beans.goods.RankBar;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADCutDetailCCompoBestCut extends SupportFragment {

    @BindView(R.id.recy)
    RecyclerView recy;
    private BargainDatailResp mBD;
    private List<RankBar> mrList = new ArrayList<>();

    public static ADCutDetailCCompoBestCut newInstance() {

        Bundle args = new Bundle();
        ADCutDetailCCompoBestCut fragment = new ADCutDetailCCompoBestCut();
        fragment.setArguments(args);
        return fragment;
    }

    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_cut_detail_a, container, false);
        L.d("hcia", "view:" + view);
        unbinder = ButterKnife.bind(this, view);


        return view;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recy.setAdapter(new CommonAdapter<RankBar>(_mActivity, R.layout.item_decut_img_c, mrList) {
            @Override
            protected void convert(ViewHolder viewHolder, final RankBar item, int position) {
                viewHolder.setText(R.id.name, item.getStart_nickname());
                viewHolder.setText(R.id.money, item.getDeduct_total_amount());
                viewHolder.setText(R.id.title, "有" + item.getHelp_number() + "位伙伴帮砍");
                ImageView imgView = viewHolder.getView(R.id.img);

                Picasso.get().load(item.getAvatar_url()).error(R.mipmap.epta).placeholder(R.mipmap.epta).fit().into(imgView);

            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(CutProduct event) {
        //好友贡献
        RetrofitClient.getClient().create(AdService.class).bargainRankingLists(event.getBargain_id(), event.getGoods_id(), mBD.getStart_activity_data().getBstart_id())
                .subscribeOn(Schedulers.newThread())

                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<BargainRankListRES>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BOb<BargainRankListRES> bargainRankListRESBOb) {

                        List<RankBar> rlist = bargainRankListRESBOb.getBody().getRanking_list();
                        mrList.clear();
                        mrList.addAll(rlist);
                        recy.getAdapter().notifyDataSetChanged();


                    }
                });
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(BargainDatailResp event) {
        mBD = event;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
